function type_id=struct_to_h5type(sobject)

%determine the size of the data type
sz=getstructsize(sobject);
if (sz==-1)
    type_id=-1;
    return;
end
type_id=gen_type(sobject,sz);
end


function sz=getstructsize(str)
%get the size of a structure mapping in bytes
fn=fieldnames(str);
sz=0;
for kk=1:length(fn)
    val=str.(fn{kk});
    switch(class(val))
        case 'double'
        if (isreal(val))
            sz=sz+8;
        else
            sz=sz+16;
        end
    case {'single','int32','uint32'}
        sz=sz+4;
    case {'int64','uint64'}
        sz=sz+8;
    
    case {'int16','uint16'}
        sz=sz+2;
   
    case {'int8','uint8','char'}
        sz=sz+1;
    case 'struct'
        nsz=getstructsize(val);
        if (nsz<0)
            sz=-1;
            return;
        end
        sz=sz+nsz;
        otherwise
            sz=-1;
            return;
    end
end
end

function tid=gen_type(sobject,sz)
%function to generate the actual type;
offset=0;
tid=H5T.create('H5T_COMPOUND',sz);
fn=fieldnames(sobject);
for kk=1:length(fn)
    val=sobject.(fn{kk});
    switch(class(val))
case 'double'
        if (isreal(val))
            H5T.insert(tid,fn{kk},offset,'H5T_NATIVE_DOUBLE');
            offset=offset+8;
        else
            datatype_id=H5T.create('H5T_COMPOUND',16);
            H5T.insert(datatype_id,'real',0,'H5T_NATIVE_DOUBLE');
            H5T.insert(datatype_id,'imag',8,'H5T_NATIVE_DOUBLE');
            H5T.insert(tid,fn{kk},offset,datatype_id);
			offset=offset+16;
        end
    case 'single'
       if (isreal(val))
            H5T.insert(tid,fn{kk},offset,'H5T_NATIVE_FLOAT');
            offset=offset+4;
        else
            datatype_id=H5T.create('H5T_COMPOUND',8);
            H5T.insert(datatype_id,'real',0,'H5T_NATIVE_FLOAT');
            H5T.insert(datatype_id,'imag',4,'H5T_NATIVE_FLOAT');
            H5T.insert(tid,fn{kk},offset,datatype_id);
			offset=offset+8;
        end
    case 'int64'
        H5T.insert(tid,fn{kk},offset,'H5T_NATIVE_LLONG');
        offset=offset+8;
    case 'uint64'
        H5T.insert(tid,fn{kk},offset,'H5T_NATIVE_ULLONG');
        offset=offset+8;
    case 'int32'
        H5T.insert(tid,fn{kk},offset,'H5T_NATIVE_INT');
        offset=offset+4;
    case 'uint32'
        H5T.insert(tid,fn{kk},offset,'H5T_NATIVE_UINT');
        offset=offset+4;
    case 'int16'
        H5T.insert(tid,fn{kk},offset,'H5T_NATIVE_SHORT');
        offset=offset+2;
    case 'uint16'
        H5T.insert(tid,fn{kk},offset,'H5T_NATIVE_USHORT');
        offset=offset+2;
    case 'int8'
        H5T.insert(tid,fn{kk},offset,'H5T_NATIVE_SCHAR');
        offset=offset+1;
    case {'uint8','char'}
        H5T.insert(tid,fn{kk},offset,'H5T_NATIVE_UCHAR');
        offset=offset+1;
    case 'struct'
        szb=getstructsize(val);
        nt=gen_type(val,szb);
        H5T.insert(tid,fn{kk},offset,nt);
        offset=offset+szb;
        
        
    end
end
end